package com.jackrain.nea.ad.service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.parser.Feature;
import com.google.common.collect.Maps;
import com.jackrain.nea.ad.api.CmenuDelCmd;
import com.jackrain.nea.ad.entity.MenuEntity;
import com.jackrain.nea.ad.mapper.CmenuMapper;
import com.jackrain.nea.ad.utility.MenuManager;
import com.jackrain.nea.common.ReflectUtils;
import com.jackrain.nea.config.Resources;
import com.jackrain.nea.cp.api.CmenuPermissionExistsQueryCmd;
import com.jackrain.nea.exception.NDSException;
import com.jackrain.nea.ext.permission.Permissions;
import com.jackrain.nea.sys.CommandAdapter;
import com.jackrain.nea.util.ApplicationContextHandle;
import com.jackrain.nea.util.ReferenceUtil;
import com.jackrain.nea.util.ValueHolder;
import com.jackrain.nea.web.query.DefaultWebEvent;
import com.jackrain.nea.web.query.QuerySession;
import com.jackrain.nea.web.query.QuerySessionImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;

/**
 * @author lzm
 * @date 2017-11-17
 */
@Slf4j
@Component
@Service(protocol = "dubbo", validation = "true", version = "1.0", group = "ad")
public class CmenuDelCmdImpl extends CommandAdapter implements CmenuDelCmd {

    @Reference(version = "1.0", group = "cp")
    private CmenuPermissionExistsQueryCmd cmd;

    @Override
    public ValueHolder execute(QuerySession session) throws NDSException {
        DefaultWebEvent event = session.getEvent();
        JSONObject param = JSON.parseObject(JSON.toJSONStringWithDateFormat(event.getParameterValue("param"),"yyyy-MM-dd HH:mm:ss"), Feature.OrderedField);
        CmenuMapper dal = ApplicationContextHandle.getBean(CmenuMapper.class);

        int count = 0;
        if(param != null){
            Long id = param.getLong("objid");
            if(id == null || id < 0){
                log.error("CmenuDelCmdImpl.execute:删除记录时没有传入objid!");
                throw new NDSException("删除记录时没有传入objid");
            }
            int existsRowsCount = dal.countMenuById(id);
            if(existsRowsCount <= 0){
                throw new NDSException(Resources.getMessage("当前记录已不存在！", session.getLocale()));
            }
            CmenuPermissionExistsQueryCmd cmd = (CmenuPermissionExistsQueryCmd)ReferenceUtil.refer(ApplicationContextHandle.getApplicationContext()
                    , CmenuPermissionExistsQueryCmd.class.getName(), "cp", "1.0");
            Long menuPermissionCount = cmd.countPermissionsByMenuId(session, id);
            if(menuPermissionCount > 0){
                throw new NDSException(Resources.getMessage("当前记录已配置了功能权限，不得删除！", session.getLocale()));
            }
            count += dal.delMenu(id);
        }

        //更新下menuTree
        List<MenuEntity> allMenus = dal.fetchAllMenus();
        List<HashMap<String, Long>> allMenuActions = dal.fetchAllActions();
        MenuManager.updateAll(session, allMenus, allMenuActions);

        ValueHolder result = new ValueHolder();
        result.put("code", 0);
        result.put("message", Resources.getMessage("删除成功的记录数："+count,session.getLocale()));
        return result;
    }
}
